<?php
/**
 * URL解析类
 *
 * @copyright (c) www.eefocus.com
 * @author kokko<kokko313@gmail.com>
 * @version 1.0 2007-11-2
 * @package com
 * @subpackage eefocus
 */
class Url{
	/**
	 * 扩展名
	 *
	 * @var string
	 * @access private
	 */
	static $_ext = '.html';
	
	/**
	 * 分割变量
	 * 
	 * @var string
	 * @access private
	 */
	static $_varDiv = "/";
	
	/**
	 * 分割参数
	 * 
	 * @var string
	 * @access private
	 */
	static $_paramDiv = "/";

	/**
	 * base路径
	 *
	 * @var string
	 * @access private
	 * @static
	 */
	static $_base;
	
	/**
	 * 解析URL path
	 * 根据path路径解析url参数
	 *
	 * @param string $path
	 * @static 
	 */
	public static function __parseURLPath( $path = '' ){
		if( $path == '')
			$path = !empty( $_SERVER['PATH_INFO'] ) ? $_SERVER['PATH_INFO'] : null;
		if( empty( $path ) ) return false;
		$var = substr( $path,1 );
		$var = substr( $var,0,strripos( $var,self::$_ext ) );
		if( self::$_varDiv==self::$_paramDiv ){
			$param = explode( self::$_paramDiv,$var );
			for( $i=0;$i<count( $param ); ){
				if( isset( $param[$i+1] ) )
					$_GET[$param[$i]] = & trim( $param[$i+1] );
				else
					$_GET[$param[$i]] = null;
				$i = $i+2;
			}
		}else{
			$param = explode( self::$_paramDiv,$var );
			foreach( $param as $val ){
				$vals = explode( self::$_varDiv,$val );
				$_GET[$vals[0]] = &trim( $vals[1] );
			}
		}
		$_REQUEST = array_merge($_REQUEST, $_GET);
	}
	
	/**
	 * 组建URL PATH重写
	 * 根据参数设置
	 *
	 * @param array $params
	 * @param string $scirpt
	 * @static 
	 */
	function __getURLPath( $params,$scirpt='' ){
		$param = array();
		$url = '';
		if( $scirpt=='' )
			$scirpt = $_SERVER['SCRIPT_NAME'];
		foreach( $params as $key=>$val ){
			$param[] = trim( $key ).self::$_varDiv.rawurlencode( $val);
		}
		$var = implode( self::$_paramDiv,$param );
		$url = $scirpt."/".$var;
		return $url.self::$_ext;
	}

	/**
	 * 将现有的url转换为PATH
	 * 
	 * @param string $url
	 * @param bool $abs 是否自动补全绝对URL
	 *	当用index.php?id=1
	 *	?id=1
	 *	../index.php?id=1
	 *	./index.php?id=1
	 *	前面没有跟路径是，必须使用绝对URL
	 * @static 
	 */
	function __convertURLToPath( $url,$abs = true ){
		if( $abs )
		$url = self::__getAbsUrl( $url );
		$url = str_replace( "?",'/',$url );
		$url = str_replace( "=",self::$_varDiv,$url );
		$url = str_replace( "&",self::$_paramDiv,$url );
		return $url.self::$_ext;
	}

	function __getAbsUrl( $url ){
		$urls = parse_url( $url );
		
		if( !isset( $urls['host'] ) )
			$urls['host'] = $_SERVER["SERVER_NAME"];

		if( !isset( $urls['scheme'] ) )
			$urls['scheme'] = "http";

		if( !isset( $urls['path'] ) )
			$urls['path'] = $_SERVER['SCRIPT_NAME'];

		$url = $urls['scheme']."://".$urls['host'];

		$start = substr( $urls['path'],0,1 );

		switch( $start ){
			case "/":	//跟路径
				$url .= $urls['path'];
				break;
			case ".":
				echo $start;
				if( substr( $urls['path'],1,2 ) == "." ){
						print_r( $_SERVER );
				}else{

					$url .= str_replace( $start,"",$urls['path'] );
				} 
				break;
			default:
				$url .= $urls['path'];
				break;

		}
		print_r( $urls );

		if( !empty( $urls['query'] ) )
			$url .= "?".$urls['query'];
		print_r( $url );
		return $url;
		
	}

	/**
	 * 将数组转换为可通过 url 传递的字符串连接
	 *
	 * 用法：
	 * <code>
	 * $string = __setArgsToParam(array('username' => 'dualface', 'mode' => 'md5'));
	 * // $string 现在为 username=dualface&mode=md5
	 * </code>
	 *
	 * @param array $args
	 * @return string
	 * @static 
	 */
	function __setArgsToParam( $args ){
		foreach( $args as $name=>$value ){
			$value = trim( $value );
			if (is_null( $value ) || $value === '') { continue; }
			$params[] = "$name=".rawurlencode( $value );
		}
		if( !empty( $params ) )
			return implode( "&",$params ); 
		return null;
	}
	
	/**
	 * url重写，结合apache重写模块使用 
	 *
	 * @param string $url
	 * @return string
	 * @static 
	 */
	function __doURLRewrite( $url ){
		$urls = parse_url( $url );
		parse_str( $urls['query'],$querys );
		$querys = implode( "_",$querys );
		if( empty( $urls['path'] ) ){
			$urls['path'] = $_SERVER['PHP_SELF'];
		}
		$urls['path'] = substr( $urls['path'],0,strrpos( $urls['path'],"." ) );
		if( !empty( $urls['scheme'] )&& !empty( $urls['host'] )  ){
			$returnUrl = $urls['scheme']."://".$urls['host'];
		}
		return $returnUrl.$urls['path']."_".$querys.".html";
	}
}
?>